﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace Ajax.Common
{
    public class CacheHelper
    {
        /// <summary> 
        /// 缓存时间方式 
        /// </summary> 
        public enum DateTimeType
        {
            /// <summary>
            /// 秒
            /// </summary>
            Second = 1,
            /// <summary> 
            /// 小时 
            /// </summary> 
            Hour = 3600,
            /// <summary> 
            /// 天数 
            /// </summary> 
            Day = 86400,
        }
        /// <summary>
        /// 新增缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="Value"></param>
        /// <param name="datetime"></param>
        public static void Add<T>(string key, T Value, int datetime)
        {
            Add<T>(key, Value, DateTimeType.Second, datetime, null, CacheItemPriority.NotRemovable);
        }
        /// <summary> 
        /// 新增缓存 
        /// </summary> 
        /// <typeparam name="T"> </typeparam> 
        /// <param name="key"> </param> 
        /// <param name="Value"> </param> 
        /// <param name="datetimetype"> </param> 
        /// <param name="datetime"> </param> 
        public static void Add<T>(string key, T Value, DateTimeType datetimetype, int datetime)
        {
            Add<T>(key, Value, datetimetype, datetime, null, CacheItemPriority.NotRemovable);
        }

        /// <summary> 
        ///  新增缓存 
        /// </summary> 
        /// <typeparam name="T"> </typeparam> 
        /// <param name="key"> </param> 
        /// <param name="Value"> </param> 
        /// <param name="datetimetype"> </param> 
        /// <param name="datetime"> </param> 
        /// <param name="file"> </param> 
        public static void Add<T>(string key, T Value, DateTimeType datetimetype, int datetime, CacheDependency file)
        {
            Add<T>(key, Value, datetimetype, datetime, file, CacheItemPriority.NotRemovable);
        }

        /// <summary> 
        /// 新增缓存 
        /// </summary> 
        /// <typeparam name="T"> </typeparam> 
        /// <param name="key"> </param> 
        /// <param name="Value"> </param> 
        /// <param name="datetimetype"> </param> 
        /// <param name="datetime"> </param> 
        /// <param name="file"> </param> 
        /// <param name="cacheItemPriority"> </param> 
        public static void Add<T>(string key, T Value, DateTimeType datetimetype, int datetime, CacheDependency file, CacheItemPriority cacheItemPriority)
        { 
            double Seconds = datetime * (int)datetimetype;

            HttpRuntime.Cache.Add(key, Value, file, DateTime.Now.AddSeconds(Seconds), TimeSpan.Zero, cacheItemPriority, null);
        }

 

        /// <summary> 
        /// 获取缓存 
        /// </summary> 
        /// <typeparam name="T"> </typeparam> 
        /// <param name="key"> </param> 
        /// <returns> </returns> 
        public static T Get<T>(string key)
        {
            return (T)HttpRuntime.Cache.Get(key);
        }
        /// <summary>
        /// 获取缓存 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool TryGet<T>(string key, out T value)
        {
            object obj = HttpRuntime.Cache.Get(key);
            if (obj == null)
            {
                value = default(T);
                return false;
            }
            else
            {
                value = (T)obj;
                return true;
            }
        }
        /// <summary> 
        /// 清空缓存 
        /// </summary> 
        public static void Clear()
        {
            HttpRuntime.Close();
        }

        /// <summary> 
        /// 移除缓存 
        /// </summary> 
        /// <param name="key"> </param> 
        public static void Remove(string key)
        {
            try
            {
                HttpRuntime.Cache.Remove(key);
            }
            catch { }
        }
    }
}